package com.quoc.activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.quoc.adapter.AdapterOffice;
import com.quoc.database.DatabaseSource;
import com.quoc.model.OfficeModel;

public class AddOffice extends Activity {

	public EditText edtMa;
	public EditText edtTen;
	public ListView listPhong;
	public List<OfficeModel> oList;
	public Button btnThem;
	public AdapterOffice aOffice;
	public DatabaseSource data;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_addoffce);
		oList = new ArrayList<OfficeModel>();
		edtMa = (EditText) findViewById(R.id.edt_maphong);
		edtTen = (EditText) findViewById(R.id.edt_tenphong);
		btnThem = (Button) findViewById(R.id.btn_addoffice);
		listPhong = (ListView) findViewById(R.id.list_office);
		DatabaseSource.instance = DatabaseSource
				.getInstance(getApplicationContext());
		oList = ReadListOffice();
		aOffice = new AdapterOffice(this, oList);
		listPhong.setAdapter(aOffice);

		aOffice.notifyDataSetChanged();

		btnThem.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ContentValues values = new ContentValues();
				int ma = Integer.parseInt(edtMa.getText().toString());
				String ten = edtTen.getText().toString();
				values.put("idOffice", ma);
				values.put("nameOffice", ten);
				if (DatabaseSource.instance.addOffice(values)) {
					addNodeInList(ma, ten);
					aOffice.notifyDataSetChanged();
					Toast.makeText(getApplicationContext(),
							"Them phong thanh cong", Toast.LENGTH_LONG).show();

				}

			}

			private void addNodeInList(int ma, String ten) {
				OfficeModel model = new OfficeModel(ma, ten);
				oList.add(model);
			}

		});

	}

	public List<OfficeModel> ReadListOffice() {
		Log.i("21106", "3");
		return DatabaseSource.instance.ReadListOffice();
	}
}
